using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Aspectize.Core;
using System.Web.Security;
using System.Collections.Specialized;
using OuSuisJe;

namespace OuSuisJeWeb
{

    [Service(Name = "OuSuisJeAuth")]
    public class OuSuisJeAuth : IAuthentication, IUserProfile, IPersistentAuthentication //, IInitializable, ISingleton
    {
        #region IAuthentication Members

        AspectizeUser IAuthentication.Authenticate(string userName, string secret, AuthenticationProtocol protocol, HashHelper.Algorithm algorithm, string chalenge)
        {

            // http://www.findwhere.me/ousuisjerest/registerservice.svc

            var bb = new System.ServiceModel.BasicHttpBinding();
            var ra = new System.ServiceModel.EndpointAddress("http://www.findwhere.me/ousuisjerest/AuthenticationService.svc");

            var membership = new OuSuisJe.AuthenticationServiceClient(bb, ra);

            var authenticated = membership.ValidateUser(userName, secret, null);

            // var authenticated = FormsAuthentication.Authenticate(userName, secret);

            if (authenticated)
            {
                IDataManager dm = EntityManager.FromDataBaseService(OuSuisJeServices.OuSuisJeStorage);
                IEntityManager em = dm as IEntityManager;

                dm.BuildDomainSchema<DomainProvider>();

                var existing = dm.GetEntities<User>(new QueryCriteria(User.Fields.UserName, ComparisonOperator.Equal, userName));

                User user;

                if (existing.Count == 0)
                {
                    user = em.CreateInstance<User>();
                    user.UserName = userName;
                    dm.SaveTransactional();
                }
                else
                {
                    user = existing[0];
                }

                return AspectizeUser.GetAuthenticatedUser(user.Id.ToString());
            }
            else
            {
                return AspectizeUser.GetUnAuthenticatedUser();
            }
        }

        #endregion

        #region IUserProfile Members

        DataSet IUserProfile.GetUserProfile()
        {
            IDataManager dm = EntityManager.FromDataBaseService(OuSuisJeServices.OuSuisJeStorage);
            IEntityManager em = dm as IEntityManager;

            dm.BuildDomainSchema<DomainProvider>();

            if (ExecutingContext.CurrentUser.IsAuthenticated)
            {
                Guid userId = new Guid(ExecutingContext.CurrentUser.UserId);

                User user = dm.GetEntity<User>(userId);
                CurrentUser cu = em.CreateInstance<CurrentUser>();
                em.AssociateInstance<IsCurrent>(user, cu);

                dm.Data.AcceptChanges();
            }

            return dm.Data;
        }

        #endregion

        #region IPersistentAuthentication Members

        bool IPersistentAuthentication.ValidateUser(AspectizeUser user)
        {
            return true;
        }

        #endregion
    }

}
